<template>
  <!-- 主要的内容区 -->
  <div class="menuCard-container">
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <template #success>
        <i class="iconfont icon-success"></i>
        加载成功
      </template>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <div
          class="menuCard"
          v-for="food in foodList"
          :key="food.id"
          @click="toDetail(food.id)"
        >
          <div class="food-card">
            <div class="food-face">
              <van-image :src="food.face" fit="cover" />
            </div>

            <div class="food-info">
              <span class="food-name">{{ food.name }}</span>
              <span class="food-functions van-multi-ellipsis--l2">
                {{ food.functions }}
              </span>
            </div>
          </div>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getFoodByFoodCate } from "@/api/food";
export default {
  name: "Menucard",

  props: {
    foodCate: {
      type: Number,
    },
  },
  data() {
    return {
      page: 1,
      foodList: [],
      loading: false,
      finished: false,
      refreshing: false,
    };
  },

  mounted() {},

  methods: {
    toDetail(foodId) {
      this.$router.push({
        name: "foodDetail",
        params: {
          foodId,
        },
      });
    },

    onLoad() {
      if (this.refreshing) {
        this.page = 1;
        this.foodList = [];
        this.refreshing = false;
      }

      let params = {
        foodCate: this.foodCate,
        page: this.page,
      };
      getFoodByFoodCate(params).then((res) => {
        if (res.data.length > 0) {
          this.foodList = [...this.foodList, ...res.data];
        } else if (res.data.length == 0) {
          this.finished = true;
        }
        this.page++;
        this.loading = false;
      });
    },
    onRefresh() {
      // 清空列表数据
      this.finished = false;

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true;
      setTimeout(() => {
        this.onLoad();
      }, 1000);
    },
  },
};
</script>

<style lang="less" scoped>
.menuCard-container {
  background-color: #f2f2f2;
}
.menuCard {
  width: 355px;
  height: auto;
  margin: 0 auto;

  .food-card {
    box-sizing: border-box;
    padding: 5px;
    width: 100%;
    height: 78px;
    background-color: #fff;
    border-radius: 10px;
    margin: 10px 0;
    display: flex;
    flex-direction: row;

    .food-face {
      width: 94px;
      height: 67px;
      border-radius: 10px;
      overflow: hidden;

      .van-image {
        width: 94px;
        height: 67px;
      }
    }

    .food-info {
      width: 243px;
      height: 100%;
      box-sizing: border-box;
      padding-left: 5px;

      span {
        display: block;
        width: 100%;
        font-size: 15px;
      }

      .food-name {
        height: 20px;
      }

      .food-functions {
        box-sizing: border-box;
        height: 47px;
        padding: 5px 0;
      }
    }
  }
}
</style>